package com.google.coffeeshop.gui.desktop.product;

import java.util.List;

import javax.swing.table.DefaultTableModel;

import com.google.coffeeshop.entity.ProductDTO;
import com.google.coffeeshop.gui.desktop.AbstractBaseTable;

public class ProductTable extends  AbstractBaseTable {

	private static final long serialVersionUID = 1L;
	
	private static String COL_PRODUCT_ID = "Product ID";
	private static String COL_IMAGE = "Image";
	private static String COL_CODE = "Code";
	private static String COL_NAME = "Name";
	
	private static Object[][] rowData = new Object[][] {};
	private static String[] columnNames = new String[] {
		COL_PRODUCT_ID, COL_IMAGE, COL_CODE,  COL_NAME
		};
	
	private DefaultTableModel model = new DefaultTableModel(
			rowData,
			columnNames
		);
	
	public ProductTable() {
		super(rowData, columnNames);
		setModel(model);
		invisibleColumn(0);
	}
	
	private void invisibleColumn(int columnIndex) {
		this.getColumnModel().removeColumn(this.getColumnModel().getColumn(columnIndex));
	}
	
	public long getProductSelected() {
		int rowIndex = this.getSelectedRow();
		return (long)findModelValue(model, COL_PRODUCT_ID, rowIndex);
	}
	private void addNewRow(long productId,String image, String code, String Name) {
		Object[] objects = new Object[] {productId,image, code, Name};
		model.addRow(objects);
	}

	private void addNewRow(ProductDTO product) {
		addNewRow(product.getId(),product.getImageFilename(), product.getCode(), product.getName());
	}
	
	public void addNewRows(List<ProductDTO> products) {
		for (ProductDTO product : products) {
			addNewRow(product);
		}
	}
	public void emptyProductTable(){
		model.setRowCount(0);
	}
}
